//
//  GalleryItem.m
//  GalleryImageDemo
//
//  Created by ju on 14-3-5.
//  Copyright (c) 2014年 Dono. All rights reserved.
//

#import "GalleryItem.h"

@implementation GalleryItem{
    NSString *_imagePath;
    NSOperationQueue *_queue;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.image = [UIImage imageNamed:@"no_pic_read"];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame imagePath:(NSString*)imagePath queue:(NSOperationQueue *)queue{
    self = [self initWithFrame:frame];
    if (self) {
        _imagePath = imagePath;
        _queue = queue;
        [self loadImage];
    }
    return self;
}

- (void)loadImage
{
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:_imagePath]];
    
    [NSURLConnection sendAsynchronousRequest:request queue:_queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError==nil) {
            if([(NSHTTPURLResponse*) response statusCode]==200){
                [self performSelectorOnMainThread:@selector(showImage:) withObject:data waitUntilDone:NO];
            }
        }else{
            //处理失败情况
            NSLog(@"%@",connectionError);
        }
        
    }];
}


- (void)showImage:(NSData*)data
{
    if ([data isKindOfClass:[NSData class]]) {
        self.image = [UIImage imageWithData:data];
    }
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
